# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=crypto++
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=8.9.0
pkgrel=1
pkgdesc="Crypto++ Library is a free C++ class library of cryptographic schemes (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.cryptopp.com/"
msys2_references=(
  "cpe: cpe:/a:cryptopp:crypto%2b%2b"
)
license=('spdx:BSL-1.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             #"${MINGW_PACKAGE_PREFIX}-doxygen"
             "unzip")
source=("https://www.cryptopp.com/cryptopp${pkgver//./}.zip"
        #https://github.com/weidai11/cryptopp/archive/CRYPTOPP_${pkgver//./_}.tar.gz
        libcrypto++.pc
        CMakeLists.txt
        cryptopp-config.cmake
        0001-mingw-clang.patch)
sha256sums=('4cc0ccc324625b80b695fcd3dee63a66f1a460d3e51b71640cdbfc4cd1a3779c'
            '60df56dcf354e8e1b24b90d460dc299c6991eb558a7068d154cb4f31be25edbd'
            '8ac69704e741805523941a75e961c6b4fb692aa1b957442c4fce783895c709c1'
            'c9356603bb5963224876a20d135a0a35192c65f7f4153374cee6e5953ff1a764'
            '83640576c8b0a366793c1a7797568a89a63e474e5852e6ce0b2c0343552ab8e7')
noextract=(cryptopp${pkgver//./}.zip)

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}

prepare() {
  [[ -d "${srcdir}"/${_realname} ]] && rm -rf "${srcdir}"/${_realname}
  unzip "${srcdir}"/cryptopp${pkgver//./}.zip -d "${srcdir}"/${_realname}

  cp -f "${srcdir}"/CMakeLists.txt "${srcdir}"/${_realname}/
  cp -f "${srcdir}"/cryptopp-config.cmake "${srcdir}"/${_realname}/

  cd "${srcdir}"/${_realname}
  apply_patch_with_msg 0001-mingw-clang.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CARCH} != x86_64 ]]; then
    _extra_config+=("-DDISABLE_SSSE3=ON" "-DDISABLE_ASM=ON")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCRYPTOPP_DATA_DIR="" \
      "${_extra_config[@]}" \
      -DBUILD_DOCUMENTATION=OFF \
      -DBUILD_TESTING=OFF \
      ../${_realname}

  ${MINGW_PREFIX}/bin/cmake --build .
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/cmake -DBUILD_TESTING=ON ../${_realname}
  ${MINGW_PREFIX}/bin/cmake --build .

  ${MINGW_PREFIX}/bin/cmake --build . --target test
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  # Install pkgconfig file
  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
  install -Dm644 "${srcdir}/libcrypto++.pc" "${pkgdir}/${MINGW_PREFIX}/lib/pkgconfig/libcrypto++.pc"
  sed -e "s|@PREFIX@|${MINGW_PREFIX}|g" \
      -e "s|@VERSION@|${pkgver}|g" \
      -i "${pkgdir}/${MINGW_PREFIX}/lib/pkgconfig/libcrypto++.pc"

  # Remove cryptest.exe and test files, only needed for check() and bloats the package
  # because cryptest.exe is linked statically.
  rm -f "${pkgdir}/${MINGW_PREFIX}/bin/cryptest.exe"
  rm -rf "${pkgdir}/${MINGW_PREFIX}/share/cryptopp/"

  install -Dm644 "${srcdir}"/${_realname}/License.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
